﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace SharpSoft.WPF.Video
{
    /// <summary>
    /// PlayerTimeShow.xaml 的交互逻辑
    /// </summary>
    public partial class PlayerTimeShow : TextBlock
    {
        Timer t;
        public PlayerTimeShow()
        {
            InitializeComponent();
            t = new Timer(900);
            t.Elapsed += T_Elapsed;
            t.AutoReset = true;
            t.Start();
        }

        private void T_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (RelevanceMeidaElement != null)
            {
                refreshTime();
            }
        }

        void refreshTime()
        {
            if (!this.Dispatcher.CheckAccess())
            {
                Dispatcher.Invoke(new Action(refreshTime));
                return;
            }
            if (RelevanceMeidaElement != null)
            {
                var hastime = false;
                try
                {
                    hastime = RelevanceMeidaElement.NaturalDuration.HasTimeSpan;
                }
                catch (Exception)
                {

                }

                if (hastime)
                {
                    string tt = RelevanceMeidaElement.NaturalDuration.TimeSpan.ToString(@"hh\:mm\:ss");
                    string nt = RelevanceMeidaElement.Position.ToString(@"hh\:mm\:ss");
                    this.Text = nt + "/" + tt;
                }
                else
                {
                    string tt = TimeSpan.FromSeconds(0).ToString(@"hh\:mm\:ss");
                    string nt = TimeSpan.FromSeconds(0).ToString(@"hh\:mm\:ss");
                    this.Text = nt + "/" + tt;
                }

            }
        }

        private MediaElement _RelevanceMeidaElement;
        /// <summary>
        /// 获取或设置关联的媒体控件
        /// </summary>
        public MediaElement RelevanceMeidaElement
        {
            get { return _RelevanceMeidaElement; }
            set
            {
                _RelevanceMeidaElement = value;
            }
        }

    }
}
